<%--
  Created by IntelliJ IDEA.
  User: zjl
  Date: 2019/11/28
  Time: 17:22
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>哈哈哈列表</title>
</head>
<%
    pageContext.setAttribute("APP_PATH",request.getContextPath());
%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<script type="text/javascript" src="${APP_PATH}/static/js/jquery-2.1.4.min.js"></script>
<link href="${APP_PATH}/static/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">
<script src="${APP_PATH}/static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
<body>
<%--搭建显示页面--%>
<div class="container">
    <%--标题--%>
    <div class="row">
        <div class="col-md-12">
            <h2>SSM_CURD</h2>
        </div>
    </div>
    <%--按钮--%>
    <div class="row">
        <div class="col-md-4 col-md-offset-8">
            <button class="btn btn-primary" id="emp_modal_button">新增</button>
            <button class="btn btn-danger">删除</button>
        </div>
    </div>
    <%--数据--%>
    <div class="row">
        <div class="col-md-12">
            <table class="table table-bordered" id="empsTable">
                <thead>
                <tr>
                    <th>#</th>
                    <th>empName</th>
                    <th>gender</th>
                    <th>email</th>
                    <th>deptName</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>

                </tbody>
            </table>
        </div>


    </div>
    <%--分页--%>
    <div class="row">
        <%--分页文字信息--%>
        <div class="col-md-6" id="page_info_area">
            当前页，总，总条记录
        </div>
        <%--分页条--%>
        <div class="col-md-6" id="page_nav_area">

        </div>

    </div>

</div>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">添加员工</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal">

                    <div class="form-group">
                        <label for="EmpName" class="col-sm-2 control-label">员工名称</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="empName" name="empName" placeholder="EmpName">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="email_text" class="col-sm-2 control-label">邮箱</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="email_text" name="email" placeholder="index@demao.com">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="email_text" class="col-sm-2 control-label">邮箱</label>
                        <div class="col-sm-10">
                            <label class="radio-inline">
                                <input type="radio" name="gender" id="inlineRadio1" value="M" checked> 男
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="gender" id="inlineRadio2" value="F"> 女
                            </label>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label">员工名称</label>
                        <div class="col-sm-4">
                            <select class="form-control" name="dId" id="dept_select">

                            </select>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button  class="btn btn-default" data-dismiss="modal">取消</button>
                <button  class="btn btn-primary" id="emp_save_btn">保存</button>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    $(function () {
        toPageData(1);
    });

    /** 数据请求*/
    function toPageData(pn) {
        $.ajax({
            url:"${APP_PATH}/emp/list",
            type:"GET",
            data:"pn="+pn,
            success:function (result) {
                if (result.flag) {
                    buildEmpListTable(result);
                    buildPageInfo(result);
                    buildPages(result);
                }else {
                    alertMsg(result.message);
                }
            }
        })
    }

    /** 解析列表数据 */
    function buildEmpListTable(result) {
        $("#empsTable tbody").empty();
        var empList = result.extendData.pageInfo.list;
        $.each(empList,function (index,item) {
            var empId = $("<td></td>").append(item.empId);
            var empName = $("<td></td>").append(item.empName);
            var gender = $("<td></td>").append(item.gender == 'M' ? "男":"女");
            var email = $("<td></td>").append(item.email);
            var deptName = $("<td></td>").append(item.deptName);
            var editBtn = $("<button></button>").addClass("btn btn-primary btn-sm").append("编辑");
            var delBtn  = $("<button></button>").addClass("btn btn-danger btn-sm").append("删除");
            var btnTd = $("<td></td>") .append(editBtn).append(delBtn);
            // append方法执行完成后返回的还是原来的元素
            $("<tr></tr>").append(empId)
                .append(empName)
                .append(gender)
                .append(email)
                .append(deptName)
                .append(btnTd)
                .appendTo("#empsTable tbody");
        })

    }

    var totalRecord = 9999;
    function buildPageInfo(result) {
        var pageInfoArea = $("#page_info_area");
        pageInfoArea.empty();
        //当前页，总，总条记录
        var pageInfo = result.extendData.pageInfo;
        pageInfoArea.append("当前"+pageInfo.pageNum+"页，")
                    .append("总"+pageInfo.pages+",")
            .append("总"+pageInfo.total+"条记录")
        totalRecord = (pageInfo.pages+1);
    }


    /** 解析分页数据 */
    function buildPages(result) {
        $("#page_nav_area").empty();
        var pageInfo = result.extendData.pageInfo;
        var ul = $("<ul></ul>").addClass("pagination");
        var firstPageLi = $("<li></li>").append($("<a></a>").append("首页").attr("href","#"));
        var prePageLi = $("<li></li>").append($("<a></a>").append("&laquo;").attr("href","#"));
        if(pageInfo.hasPreviousPage == false){
            firstPageLi.addClass("disabled");
            prePageLi.addClass("disabled");
        }else {
            // 首页点击事件
            firstPageLi.click(function () {
                toPageData(1);
            });
            //前一页点击事件
            prePageLi.click(function () {
                toPageData(pageInfo.pageNum - 1);
            });
        }
        // 添加首页和前一页
        ul.append(firstPageLi).append(prePageLi);
        // 遍历给ul中添加页码
        $.each(pageInfo.navigatepageNums,function (index,num) {
            var pageNumLi = $("<li></li>").append($("<a></a>").append(num).attr("href", "#"));
            if(pageInfo.pageNum == num) {
                 pageNumLi.addClass("active");
            }
            pageNumLi.click(function () {
                toPageData(num);
            });
            ul.append(pageNumLi);
        });
        var nextPageLi = $("<li></li>").append($("<a></a>").append("&raquo;").attr("href","#"));
        var lastPageLi = $("<li></li>").append($("<a></a>").append("尾页").attr("href","#"));
        if(pageInfo.hasNextPage == false){
            nextPageLi.addClass("disabled");
            lastPageLi.addClass("disabled");
        }else {
            //下一页点击事件
            nextPageLi.click(function () {
                toPageData(pageInfo.pageNum + 1);
            });
            // 尾页点击事件
            lastPageLi.click(function () {
                toPageData(pageInfo.pages);
            });
        }
        // 添加后一页和尾页
        ul.append(nextPageLi).append(lastPageLi);
        var nav = $("<nav></nav>").append(ul);
        nav.appendTo("#page_nav_area")

    }

    /**
     * 新增按钮
     */
    $("#emp_modal_button").click(function () {
        $.ajax({
            url:"${APP_PATH}/dept/list",
            type:"GET",
            success:function (result) {
                console.log(result)
                getDeptList(result);
            }
        });
        $('#myModal').modal({
            backdrop:"static"
        })
    });

    // 查询
    function getDeptList(result) {
        $("#dept_select").empty();
        var deptList = result.extendData.deptList;
        $.each(deptList,function () {
            var option =  $("<option></option>").append(this.deptName).attr("value",this.deptId);
            $("#dept_select").append(option);
        })
    }

    //保存
    $("#emp_save_btn").click(function () {
        if(!validate_add_form){
            return false;
        }
        $.ajax({
            url:"/emp/emp",
            type:"POST",
            data: $("#myModal form").serialize(),
            success:function (result) {
                // 执行状态
                alert(result.message);
                // 关闭模态框
                $('#myModal').modal('hide');
                // 定位最后一页数据
                toPageData(totalRecord);
            }
        })
    });

    // 表单数据校验
    function validate_add_form() {
        var empName = $("#empName").val();
        var regName = "/(^[a-zA-Z0-9_-]{3,16}$)/"
        if (!regName.test(empName)){
            alert()
        }
    }

    /** 信息提示 */
    function alertMsg(msg) {
        alert(msg)
    }

</script>
</body>
</html>
